﻿// <copyright file="OclOperationInstaller.cs" company="Codeplex project Eco.Contrib"> 
// The MIT License (MIT) 
// </copyright> 
// <author>Peter Buchmann</author> 

namespace Eco.Contrib
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Eco.ObjectRepresentation;
    using Eco.Ocl.Support;
    using Eco.Services;

    /// <summary>
    /// static class that is able to install all OCL functions of the assembly
    /// </summary>
    public static class OclOperationInstaller
    {
        /// <summary>
        /// operationTypes field
        /// </summary>
        private static List<Type> operationTypes = typeof(OclOperationInstaller).Assembly.GetExportedTypes()
            .Where(x => typeof(OclOperationBase).IsAssignableFrom(x)).ToList();

        /// <summary>
        /// Installs are OCL functions that are defined in the current assembly.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns>Installed operations</returns>
        public static string Install(IEcoServiceProvider serviceProvider)
        {
            string operations = string.Empty;
            if (serviceProvider != null)
            {
                IOclService oclService = serviceProvider.GetEcoService<IOclService>();
                IActionLanguageService actionLanguageService = serviceProvider.GetEcoService<IActionLanguageService>();
                operationTypes.ForEach(x =>
                {
                    OclOperationBase operation = Activator.CreateInstance(x) as OclOperationBase;
                    oclService.InstallOperation(operation);
                    actionLanguageService.InstallOperation(operation);
                    operations += " " + operation.Name;
                });
            }
            else
            {
                throw new EcoContribException("Parameter not set.");
            }

            return operations;
        }
    }
}
